﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common.Web;

using Jzy.Model.Account;
using Jzy.BLL.Account;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;


namespace Jzy.Web.Account
{
    public partial class Edit : PageEdit
    {
        private AccountBO Account
        {
            get
            {
                return AccountBO.Instance<AccountBO>();
            }
        }

        private string IsIncome
        {
            get
            {
                return Request.QueryString["IsIncome"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DictBO dict = new DictBO();
                if (IsIncome == "1")
                {
                    lblType.Text = "收入类型";
                    dict.BindListControl(TypeCode, (int)DictItem.收入类型);
                }
                else
                {
                    lblType.Text = "支出类型";
                    dict.BindListControl(TypeCode, (int)DictItem.支出类型);
                }



                if (string.IsNullOrEmpty(PKID))
                {
                    OccurTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                else
                {
                    DispInfo();
                }
            }
            AddAttr();
            this.IncludeJsFile("calendar", JsFile.Calendar);
        }

        private void DispInfo()
        {
            AccountInfo info = Account.GetInfo(PKID);
            SetControl(pnlMain, info);
        }

        private void AddAttr()
        {
            WebUtil.SetTimeBox(OccurTime, false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Amount.Text.Trim() == "")
            {
                Alert("请输入发生额");
                return;
            }

            AccountInfo info = Account.GetInfo(this.PKID);
            info.Amount = decimal.Parse(Amount.Text);

            if (this.IsIncome == "0")
            {
                info.Amount = -Math.Abs(info.Amount);
            }

            info.CreatorID = AppSession.UserID;
            info.CreatorName = AppSession.UserName;
            if (string.IsNullOrEmpty(PKID))
            {
                info.InputTime = DateTime.Now;
            }
            info.IsIncome = this.IsIncome == "1";
            info.Name = Name.Text.Trim();
            info.OccurTime = DateTime.Parse(OccurTime.Text);
            info.TypeCode = int.Parse(TypeCode.SelectedValue);
            info.TypeName = TypeCode.SelectedItem.Text;

            this.Account.Save(info);

            if (string.IsNullOrEmpty(PKID))
            {
                Clear();
                JSUtil.RefreshView(this);
            }
            else
            {
                JSUtil.RefreshView();
            }
        }

        private void Clear()
        {
            Name.Text = Amount.Text = "";
            OccurTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

        }
    }
}